package com.ruixinzheng.databindingdemo.viewmodel;

import android.util.Log;

import androidx.databinding.Observable;
import androidx.databinding.ObservableField;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.ruixinzheng.databindingdemo.CustomObservable;

import java.util.Random;

public class MainViewModel extends ViewModel {
    private static final String TAG = MainViewModel.class.getSimpleName();

    private MutableLiveData<String> mutableLiveData = new MutableLiveData<>("MutableLiveData");
    private ObservableField<String> observableField = new ObservableField<>("ObservableField");
    private CustomObservable customObservable = new CustomObservable();

    public MainViewModel() {
        observableField.addOnPropertyChangedCallback(new Observable.OnPropertyChangedCallback() {
            @Override
            public void onPropertyChanged(Observable sender, int propertyId) {
                Log.i(TAG, "observableField changed: propertyId " + propertyId);
            }
        });
        customObservable.addOnPropertyChangedCallback(new Observable.OnPropertyChangedCallback() {
            @Override
            public void onPropertyChanged(Observable sender, int propertyId) {
                Log.i(TAG, "customObservable changed: propertyId " + propertyId);
            }
        });
    }

    public void onClick() {
        String value = String.valueOf(new Random().nextInt());
        mutableLiveData.setValue(value);
        observableField.set(value);
        customObservable.setStr(value);
    }

    public CustomObservable getCustomObservable() {
        return customObservable;
    }

    public MutableLiveData<String> getMutableLiveData() {
        return mutableLiveData;
    }

    public ObservableField<String> getObservableField() {
        return observableField;
    }
}